<?php

namespace app\api\controller\Product;

use think\Controller;

class Cart extends Controller
{
	
	public function __construct()
	{
	    parent::__construct();
		
		$this->CartModel = model('Product.Cart');
		$this->ProductModel = model('Product.Product');
	}
	
	public function nums()
	{
		$nums = $this->request->param('nums',0);

		$id = $this->request->param('id',0);

		$cart = $this->CartModel->find($id);

		if(!$cart)
		{
			$this->error('该商品未加入购物车里');
		}

		$data = [
			'id' => $id,
			'nums' => $nums,
			'total' => bcmul($cart['price'],$nums)
		];

		$result = $this->CartModel->isUpdate(true)->save($data);

		if($result === FALSE)
		{
			$this->error('更新该商品数量失败');
		}else{
			$this->success('更新成功');
		}

	}
	
    public function add()
    {
        $userid = $this->request->param('userid');
        $proid = $this->request->param('proid');
        $num = $this->request->param('num');

        $product = model('Product.Product')->find($proid);

        if($product['stock'] <= 0)
        {
            $this->error('该商品库存不足');
        }

        // $user = model('User.User')->where(['id' => $userid])->value();


        $data = [
            'userid' => $userid,
            'proid' => $proid,
            'nums' => $num,
            'price' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
            'total' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price']
        ];

        $Cart = model('Product.Cart')->where(['userid' => $userid,'proid' => $proid])->find();
		
		if(!empty($Cart))
		{
			// 能查询购物车的确存在该商品 => 更新数据

			// 商品数量
			$nums = bcadd($Cart['nums'],$num);

			$UpdateData = [
				'id' => $Cart['id'],
				'nums' => $nums,
				'total' => bcmul($data['price'],$nums)
			];

			$result = $this->CartModel->isUpdate(true)->save($UpdateData);
		}else{
			// 查询到购物车没有该商品 => 添加
			$result = $this->CartModel->validate('common/Product/Cart')->save($data);
		}
		
		if($result === FALSE)
		{
			$this->error('加入失败');
		}
		
		$this->success('加入成功');
        
    }
	
	public function info()
	{
		$userid = $this->request->param('userid',0);
		
		$cart = model('Product.Cart')->where('userid',$userid)->select();
		
		if(!$cart)
		{
			$this->error('您还未加入任何商品');
		}
		
		$cartlist = model('Product.Cart')->with(['product'])->where('userid',$userid)->select();
		
		$this->success('查询成功',null,$cartlist);
		
	}
	
	// 删除购物车
	public function del()
	{
		$id = $this->request->param('id');
		
		$Cart = $this->CartModel->find($id);
		
		if(!$Cart)
		{
			$this->error('该购物车为空');
		}
		
		$result = $this->CartModel->where('id',$id)->delete();
		
		if($result === FALSE)
		{
			$this->error('删除失败');
		}
		
		$this->success('删除成功');
		
	}
	// 查询所选提交的订单商品
	public function list()
	{
		$userid = $this->request->param('userid');
		// 从购物车提交订单
		$cartids = $this->request->param('ids');
		// 从立即购买提交订单
		$proid = $this->request->param('proid');
		
		if($cartids != 0)
		{
			$list = $this->CartModel->with(['product'])->whereIn('cart.id',$cartids)->select();
			
			if(!$list)
			{
				$this->error('所选的商品不存在，请重新选择');
			}
			
			$this->success('查询成功',null,$list);
		}else{
			$list = $this->ProductModel->where('id',$proid)->select();
			
			if(!$list)
			{
				$this->error('所选的商品不存在，请重新选择');
			}
			
			$this->success('查询成功',null,$list);
		}
		
	}
	
}
